package com.xinpu.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xinpu.entity.TCompanyCdrClassify;
import com.xinpu.entity.TCompanyCdrText;
import com.xinpu.entity.TCompanyCdrVideoImg;
import com.xinpu.service.ITCompanyCdrClassifyService;
import com.xinpu.service.ITCompanyCdrTextService;
import com.xinpu.service.ITCompanyCdrVideoImgService;
import com.xinpu.vo.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cdrClassify")
public class CdrClassifyController {

    @Autowired
    private ITCompanyCdrClassifyService itCompanyCdrClassifyService;
    @Autowired
    private ITCompanyCdrTextService itCompanyCdrTextService;
    @Autowired
    private ITCompanyCdrVideoImgService itCompanyCdrVideoImgService;

    /**
     * 分类列表
     *
     * @return
     */
    @RequestMapping("/list")
    public ResponseMessage list(@RequestParam(value = "type", required = false) Integer type) {
        return ResponseMessage.ok(itCompanyCdrClassifyService.list(Wrappers.<TCompanyCdrClassify>lambdaQuery().eq(TCompanyCdrClassify::getType, type)));
    }

    /**
     * 编辑或者新增
     *
     * @param map
     * @return
     */
    @RequestMapping("/saveOrUpate")
    public ResponseMessage saveOrUpate(@RequestBody Map<String, String> map) {
        TCompanyCdrClassify tCompanyCdrClassify = new TCompanyCdrClassify();
        String id = map.get("id");
        TCompanyCdrClassify companyCdrClassify = itCompanyCdrClassifyService.getOne(Wrappers.<TCompanyCdrClassify>lambdaQuery().eq(TCompanyCdrClassify::getName, map.get("name")));
        if (StringUtils.isNotBlank(id)) {
            tCompanyCdrClassify.setId(new Integer(id));
            if (companyCdrClassify != null) {
                if (companyCdrClassify.getId().toString().equals(id)) {
                    return ResponseMessage.error("名称重复,请重新提交");
                }
                tCompanyCdrClassify.setName(map.get("name"));
                tCompanyCdrClassify.setType(new Integer(map.get("type")));
                tCompanyCdrClassify.setPid(0);
                return ResponseMessage.ok(itCompanyCdrClassifyService.saveOrUpdate(tCompanyCdrClassify));
            }
        }
        if (companyCdrClassify == null) {
            tCompanyCdrClassify.setName(map.get("name"));
            tCompanyCdrClassify.setType(new Integer(map.get("type")));
            tCompanyCdrClassify.setPid(0);
            return ResponseMessage.ok(itCompanyCdrClassifyService.saveOrUpdate(tCompanyCdrClassify));
        }
        return ResponseMessage.error("名称重复,请重新提交");
    }

    /**
     * 删除
     *
     * @param map
     * @return
     */
    @RequestMapping("/remove")
    public ResponseMessage remove(@RequestBody Map<String, Integer> map) {
        if (map.get("type") == 0) {
            List<TCompanyCdrText> texts = itCompanyCdrTextService.list(Wrappers.<TCompanyCdrText>lambdaQuery().eq(TCompanyCdrText::getCid, map.get("id")));
            for (TCompanyCdrText text : texts) {
                text.setCid(0);
                itCompanyCdrTextService.updateById(text);
            }
        }
        if (map.get("type") == 1) {
            List<TCompanyCdrVideoImg> imgs = itCompanyCdrVideoImgService.list(Wrappers.<TCompanyCdrVideoImg>lambdaQuery().eq(TCompanyCdrVideoImg::getCid, map.get("id")));
            for (TCompanyCdrVideoImg img : imgs) {
                img.setCid(0);
                itCompanyCdrVideoImgService.updateById(img);
            }
        }
        return ResponseMessage.ok(itCompanyCdrClassifyService.removeById(map.get("id")));
    }
}
